#include "user_main.h"
#include "string.h"
#include "ti_msp_dl_config.h"
#include "order_sensor_bsp.h"

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// UART

volatile uart_data_t g_uart_data_usb = 
{
    .uart_port  = UART_PORT_USB,
    
    .send_len = 0,
    .send_index = 0,
    .send_flag  = 0,

    .recv_index = 0,
    .recv_flag  = 0,
};

volatile uart_data_t g_uart_data_udp = 
{
    .uart_port  = UART_PORT_UDP,

    .send_len = 0,
    .send_index = 0,
    .send_flag  = 0,

    .recv_index = 0,
    .recv_flag  = 0,    
};


void UART_0_INST_IRQHandler(void)
{
    switch (DL_UART_Main_getPendingInterrupt(UART_0_INST)) {
        case DL_UART_MAIN_IIDX_RX:
            g_uart_data_usb.recv_buf[g_uart_data_usb.recv_index++] = DL_UART_Main_receiveData(UART_0_INST);
            g_uart_data_usb.recv_flag = 1;
            bsp_timing_uart_usb_receive( IDLE );
            break;

        case DL_UART_MAIN_IIDX_EOT_DONE:
            if (g_uart_data_usb.send_index < g_uart_data_usb.send_len)
            {
                DL_UART_transmitData(UART_0_INST, g_uart_data_usb.send_buf[g_uart_data_usb.send_index++]);
            }
            else
            {
                g_uart_data_usb.send_index = 0;
                g_uart_data_usb.send_len = 0;
            }
            break;

        default:
            break;
    }
}

void UART_1_INST_IRQHandler(void)
{
    switch (DL_UART_Main_getPendingInterrupt(UART_1_INST)) {
        case DL_UART_MAIN_IIDX_RX:
            g_uart_data_udp.recv_buf[g_uart_data_udp.recv_index++] = DL_UART_Main_receiveData(UART_1_INST);
            g_uart_data_udp.recv_flag = 1;
            bsp_timing_uart_udp_receive( IDLE );
            break;

        case DL_UART_MAIN_IIDX_EOT_DONE:
            if (g_uart_data_udp.send_index < g_uart_data_udp.send_len)
            {
                DL_UART_transmitData(UART_1_INST, g_uart_data_udp.send_buf[g_uart_data_udp.send_index++]);
            }
            else
            {
                g_uart_data_udp.send_index = 0;
                g_uart_data_udp.send_len = 0;
            }
            break;

        default:
            break;
    }
}

uint8_t bsp_uart_send( uart_data_t *uart_data )
{
    if( uart_data->uart_port == UART_PORT_USB )
    {
        g_uart_data_usb.recv_index = 0;
        g_uart_data_usb.recv_len = 0;

        g_uart_data_usb.send_index = 0;

        if(uart_data->send_len > 0)
            DL_UART_transmitData(UART_0_INST, g_uart_data_usb.send_buf[g_uart_data_usb.send_index++]);
    }
    else if( uart_data->uart_port == UART_PORT_UDP )
    {
        g_uart_data_udp.recv_index = 0;
        g_uart_data_udp.recv_len = 0;
        
        g_uart_data_udp.send_index = 0;
        if(uart_data->send_len > 0)
            DL_UART_transmitData(UART_1_INST, g_uart_data_udp.send_buf[g_uart_data_udp.send_index++]);
    }

    return 0;
}

uint8_t bsp_uart_recv_flag( uart_data_t *uart_data)
{

    return uart_data->recv_flag;
}

void bsp_uart_clear_send_buf(  uart_data_t *uart_data )
{
    uart_data->send_index = 0;
    uart_data->send_len = 0;
    memset(uart_data->send_buf, 0, sizeof(uart_data->send_buf));
    uart_data->send_flag = UART_FLAG_IDLE;
}

void bsp_uart_clear_recv_buf(  uart_data_t *uart_data )
{
    uart_data->recv_index = 0;
    uart_data->recv_len = 0;
    memset(uart_data->recv_buf, 0, sizeof(uart_data->recv_buf));
    uart_data->recv_flag = UART_FLAG_IDLE;
}

uint32_t bsp_timing_uart_usb_receive ( uint8_t flag)
{
    static uint8_t _current_state = IDLE;
    static uint32_t _start_ticks = 0;

    if( _current_state == IDLE && flag == IDLE )
    {
        return 0;
    }
    else if( _current_state == IDLE && flag == RUN )
    {
        _current_state = RUN;
        _start_ticks = g_ticks_inc;
        return 0;
    }
    else if( _current_state == RUN && flag == IDLE )
    {
        _current_state = IDLE;
        return (g_ticks_inc - _start_ticks);
    }
    else if( _current_state == RUN && flag == RUN )
    {
        return (g_ticks_inc - _start_ticks);
    }
    return 0;

}

uint32_t bsp_timing_uart_udp_receive ( uint8_t flag)
{
    static uint8_t _current_state = IDLE;
    static uint32_t _start_ticks = 0;

    if( _current_state == IDLE && flag == IDLE )
    {
        return 0;
    }
    else if( _current_state == IDLE && flag == RUN )
    {
        _current_state = RUN;
        _start_ticks = g_ticks_inc;
        return 0;
    }
    else if( _current_state == RUN && flag == IDLE )
    {
        _current_state = IDLE;
        return (g_ticks_inc - _start_ticks);
    }
    else if( _current_state == RUN && flag == RUN )
    {
        return (g_ticks_inc - _start_ticks);
    }
    return 0;

}

uint32_t bsp_timing_uart_receive( uart_data_t *uart_data , uint8_t flag )
{
    if( uart_data->uart_port == UART_PORT_USB )
    {
        return bsp_timing_uart_usb_receive( flag );
    } 

    if( uart_data->uart_port == UART_PORT_UDP )
    {
        return bsp_timing_uart_udp_receive( flag );
    }
    return 0; 
}



uint32_t bsp_timing_setup_wifi_udp( uint8_t flag )
{
    static uint8_t _current_state = IDLE;
    static uint32_t _start_ticks = 0;

    if( _current_state == IDLE && flag == IDLE )
    {
        return 0;
    }
    else if( _current_state == IDLE && flag == RUN )
    {
        _current_state = RUN;
        _start_ticks = g_ticks_inc;
        return 0;
    }
    else if( _current_state == RUN && flag == IDLE )
    {
        _current_state = IDLE;
        return (g_ticks_inc - _start_ticks);
    }
    else if( _current_state == RUN && flag == RUN )
    {
        return (g_ticks_inc - _start_ticks);
    }
    return 0;

}

